package com.lxb.demo.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.Map;

/**
 * @Author lxb
 * @Date 2020/10/21 10:51
 * @Version 1.0
 */
public class JwtUtils {

    private static String SING = "!tgf@#4$7@573*85";

    /**
     * 生成token
     * @param map
     * @return
     */
    public static String getToken(Map<String,String> map){
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.DATE,7);
        JWTCreator.Builder builder= JWT.create();
        map.forEach((k,v) ->{
            builder.withClaim(k,v);
        });
        String token = builder.withExpiresAt(instance.getTime()).sign(Algorithm.HMAC256(SING));//设置盐

        return token;
    }

    /**
     * 验证token合法性
     * @param token
     */
    public static DecodedJWT verify(String token){
      return JWT.require(Algorithm.HMAC256(SING)).build().verify(token);
    }

}
